local _M = {}

do
  function _M.setup_test_case(redis, test_case_file)
    local file = io.input(test_case_file)
    repeat
      local line = io.read()
      if nil == line then
        break
      end
      if type(line) == "string"  and   string.len(line) > 0 then
        if string.byte(line) ~= string.byte('#') then
          local quotation = nil
          local token  = nil
          local tokens = {}
          string.gsub(line..'\0', ".",
            function(c)
              if c == "\0" then
                if quotation then
                  error("missing closing qutation")
                else
                  if token then
                      table.insert(tokens, token)
                      token = nil
                  end
                  return ""
                end
              elseif c == "'" or c == '"' then
                if not quotation then
                  quotation = c
                  token = ""
                elseif quotation == c then
                  table.insert(tokens, token)
                  token = nil
                  quotation = nil
                else
                  token = token .. c
                end
              elseif c == " " then
                if not quotation then
                  if token then
                    table.insert(tokens, token)
                    token = nil
                  end
                else
                  token = (token or "") .. c
                end
              else
                token = (token or "") .. c
              end
            end)
          if next(tokens) then
            local cmd = tokens[1]
            redis[cmd](redis, unpack(tokens, 2))
          end
        end
      end
    until (false)
    io.close(file)
  end


  function _M.teardown_test_case(redis, ...)
    for _, db in ipairs({...}) do
      redis:SELECT(db)
      redis:FLUSHDB()
    end
  end
end

return _M